/*
 * Copyright 2008-2009 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.task.retrieval;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.cmu.lti.sepia.data.Lang;
import edu.cmu.lti.sepia.task.nlp.TextProcessor;

public class DocumentFormatter {

//    private static Pattern pDeleteTag = Pattern.compile("(<WORDS>.*?</WORDS>)|(<LANG>.*?</LANG>)|(<AE>.*?</AE>)|(<SECTION>.*</SECTION>)", Pattern.DOTALL+Pattern.CASE_INSENSITIVE);
//    private static Pattern pSpaces1 = Pattern.compile("(\n[ ]?)+", Pattern.DOTALL);
//    private static Pattern pSpaces1 = Pattern.compile("\n", Pattern.DOTALL);
    private static Pattern pSpaces2 = Pattern.compile("^[ \n\t]+|[ \n\t]+?$", Pattern.DOTALL);
    private static Pattern pContent = Pattern.compile("<(?:HEADLINE|TEXT).*?>(.+?)</(?:HEADLINE|TEXT)>",Pattern.DOTALL+Pattern.CASE_INSENSITIVE);
    private static int min = 120;
    private static int block = 30;   
    
    public static String getSnippet( String document, String[] queryTerms ) {
    	document = document.replaceAll("\n", " ");
    	document = document.replaceAll("[　]+", "　"); //zenkaku space
    	String snippet = ""; // no need to use stringbuilder
    	Matcher mContent = pContent.matcher(document);
    	while ( mContent.find() ) {
    		snippet += mContent.group(1);
    	}
    	snippet = cleanUp(snippet);
    	
    	boolean[] offsets = new boolean[snippet.length()];
    	
    	for ( String qt : queryTerms ) {
    		int start = 0;
    		while ( start < snippet.length()-1 ) {
    			int offset = snippet.toLowerCase().indexOf(qt.toLowerCase(), start);
    			if ( offset == -1 ) break;
    			Arrays.fill(offsets, offset, offset+qt.length(), true);
    			start = offset+qt.length();
    		}
    	}
    	
    	int max = 0;
    	int argmax = 0;
    	for ( int i=0; i<offsets.length/block; i++ ) {
    		int sum = 0;
    		for ( int j=0; j<block && i*block+j<offsets.length; j++ ) {
    			if (offsets[i*block+j]) sum++;
    		}
    		if ( sum > max ) {
    			max = sum;
    			argmax = i;
    		}
    	}
    	return snippet.substring( argmax*block, Math.min(argmax*block+min, snippet.length()) );
//    	return snippet.substring( argmax*30, Math.min(argmax*30+min, snippet.length()) );
    }
	
  public static String getCleanDocument(String langString, String doc) {
    Lang lang = Lang.valueOf(langString.toUpperCase());
    
    if (lang != null && !lang.isSpaceSeparatedLanguage()) {
      doc = TextProcessor.unsegment(doc);
    }
 // New policy: Keep the document as is
    //doc = doc.replaceAll("<P>|</P>", " ");
    //doc = pDeleteTag.matcher(doc).replaceAll("");
//    doc = pSpaces1.matcher(doc).replaceAll("\n");
    doc = pSpaces2.matcher(doc).replaceAll("");
    return doc;
  }

  public static String cleanUp(String text) {
    return text.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    //.replaceAll("\n", "<br>") // New policy: Keep the document as is
  }

}
